package com.blue.controller.author;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.annotation.NotNull;
import com.blue.common.constant.CommonConstant;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ConvertUtils;
import com.blue.common.utils.QueryWrapperUtils;
import com.blue.dto.author.MenuDTO;
import com.blue.dto.author.RoleDTO;
import com.blue.po.author.MenuPO;
import com.blue.service.author.IMenuService;
import com.blue.vo.author.MenuVO;
import com.blue.vo.author.RoleVO;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 菜单表
 *
 * @author cnj
 * @email 15696471158@163.com
 * @date 2024-03-17
 */
@RestController
@RequestMapping("/author/menu")
@Tag(name = "菜单表")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    public ResponseResult<?> list(MenuDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        MenuPO MenuPO = ConvertUtils.convert(MenuPO.class, requestDTO);
        MPJQueryWrapper<MenuPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(MenuPO);
        Page<MenuPO> page = new Page<>(pageNum, pageSize);
        Page<MenuPO> pageResult = menuService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody MenuDTO requestDTO) {
        MenuPO MenuPO = ConvertUtils.convert(MenuPO.class, requestDTO);
        if(StringUtils.isBlank(MenuPO.getParentId())){
            MenuPO.setParentId(CommonConstant.ROOT);
        }
        menuService.save(MenuPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody MenuDTO requestDTO) {
        MenuPO MenuPO = ConvertUtils.convert(MenuPO.class, requestDTO);
        menuService.updateById(MenuPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        menuService.removeByIds(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody MenuDTO requestDTO) {
        MenuPO MenuPO = menuService.getById(requestDTO.getMenuId());
        return ResponseResult.okResult(MenuPO);
    }

    @GetMapping("/menuList")
    @Operation(summary = "获取菜单列表")
    public ResponseResult<?> menuList(@RequestParam(required = false) String roleId) {
        List<MenuVO> responseResult = menuService.menuList(roleId);
        return ResponseResult.okResult(responseResult);
    }

    @PostMapping("/queryMenu")
    @Operation(summary = "查询菜单")
    public ResponseResult<?> queryMenu(@RequestBody MenuDTO requestDTO) {
        List<MenuVO> responseResult = menuService.queryMenu(requestDTO);
        return ResponseResult.okResult(responseResult);
    }
    @PostMapping("/authorMenuList")
    @Operation(summary = "获取已授权菜单")
    @NotNull(value = "roleId", message = "角色ID不能为空！")
    public ResponseResult<?> authorMenuList(@RequestBody RoleDTO requestDTO) {
        Map<String, List<String>> resultMap = menuService.authorMenuList(requestDTO);
        return ResponseResult.okResult(resultMap);
    }
}
